package com.example.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.common.ResultMessage;
import com.example.backend.entity.DiZhi;
import com.example.backend.service.IDiZhiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dizhi")
public class DiZhiController {
    @Autowired
    private IDiZhiService diZhiService;

    @PostMapping
    public ResultMessage addDizhi(@RequestBody DiZhi diZhi){
        if (diZhiService.addDiZhi(diZhi)){
            return ResultMessage.success(null,"添加成功");
        }else {
            return ResultMessage.error("添加失败");
        }
    }
    @DeleteMapping("/{dzid}")
    public ResultMessage deleteDizhi(@PathVariable int dzid){
        if (diZhiService.deleteDiZhi(dzid)){
            return ResultMessage.success(null,"删除成功");
        }else {
            return ResultMessage.error("删除失败");
        }
    }
    @PutMapping
    public ResultMessage updateDizhi(@RequestBody DiZhi diZhi){
        if (diZhiService.updateDiZhi(diZhi)){
            return ResultMessage.success(null,"修改成功");
        }else {
            return ResultMessage.error("修改失败");
        }
    }

    @GetMapping("/{dzid}")
    public ResultMessage queryDizhiById(@PathVariable int dzid){
        return ResultMessage.success(diZhiService.queryDiZhiById(dzid),"查询成功");
    }
    //@GetMapping
    //public ResultMessage queryDizhi(){
     //   return ResultMessage.success(diZhiService.queryDiZhi(),"查询成功");
   // }

    @GetMapping("/list")
    public ResultMessage queryDizhiByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder,
            DiZhi diZhi) {
        // 调用分页查询方法（需要在 Service 中实现）
        Page<DiZhi> pageResult = diZhiService.queryDiZhiByPage(
                diZhi, page, size, sortField, sortOrder);

        return ResultMessage.success(pageResult, "查询成功");
    }

    @GetMapping("/byYhid/{yhid}")
    public ResultMessage queryDizhiByYHId(@PathVariable("yhid") Integer yhid) {
        return ResultMessage.success(diZhiService.queryDizhiByYHId(yhid), "查询成功");
    }
}
